package org.thulearn.htmlparser;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.thulearn.model.User;

public class ParseUserInfo {
	static public void parseUserInfo(String html, User user) {
		Parser userParser = Parser.createParser(html, "utf-8");
		
		try {
			NodeList tableList = userParser.extractAllNodesThatMatch(new NodeFilter(){
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public boolean accept(Node node) {
					if(node instanceof TableTag) {
						return true;
					} else { 
						return false;
					}
				}
			});
			
			for(int i = 0; i < tableList.size(); i++) {
				TableTag table = (TableTag) tableList.elementAt(i);
				
				if(i == 1) {
					TableRow row[] = table.getRows();
					TableColumn column[] = row[0].getColumns();
					String id = column[1].toPlainTextString().trim();
					column = row[1].getColumns();
					String name = column[1].toPlainTextString().trim();
					
					user.setId(id);
					user.setName(name);
					
					break;
				}
			}
			
		} catch (ParserException e) {
			e.printStackTrace();
		}
	}
}
